package xyz.wjwnya.scoring_module.service.impl;

import org.springframework.stereotype.Service;
import xyz.wjwnya.common.auth.PaginationData;
import xyz.wjwnya.common.auth.QueryReturnDate;
import xyz.wjwnya.common.auth.UserSessionData;
import xyz.wjwnya.common.exception.UserNotLoginException;
import xyz.wjwnya.common.exception.WrongPaginationException;
import xyz.wjwnya.database.entity.score.GroupScore;
import xyz.wjwnya.database.entity.score.PersonalScore;
import xyz.wjwnya.database.entity.score.ScoreQueryCriteria;
import xyz.wjwnya.database.mapper.ScoreMapper;
import xyz.wjwnya.scoring_module.service.ScoreService;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    private final ScoreMapper scoreMapper;

    public ScoreServiceImpl(ScoreMapper scoreMapper) {
        this.scoreMapper = scoreMapper;
    }
    //查询学生个人综合评分
    @Override
    public QueryReturnDate QueryPersonalScoreCondition(UserSessionData userSessionData, int number, int page, int courseId, String projectGrupId) {
        //用户登录信息是否存在
        if (userSessionData == null) throw new UserNotLoginException();
        //分页参数是否正确
        if(number<1||page<1) throw  new WrongPaginationException();
        //limit偏移量
        int pageIndex = page==1?0:page*number;
        //查询项目组评分 mybatis
        List<Object> scores = scoreMapper.QueryPersonalScoreCondition(new ScoreQueryCriteria(courseId,projectGrupId,userSessionData.getAccount(),userSessionData.getUserType(),pageIndex,number));
        return new QueryReturnDate(new PaginationData(number,page,((List<Integer>) scores.get(1)).get(0)),(List<PersonalScore>)scores.get(0));
    }
    //查询项目组综合评分
    @Override
    public QueryReturnDate QueryGroupScoreCondition(UserSessionData userSessionData, int number, int page, int courseId, String projectGrupId) {
        //用户登录信息是否存在
        if (userSessionData == null) throw new UserNotLoginException();
        //分页参数是否正确
        if(number<1||page<1) throw  new WrongPaginationException();
        //limit偏移量
        int pageIndex = page==1?0:page*number;
        //查询项目组评分 mybatis
        List<Object> scores = scoreMapper.QueryGroupScoreCondition(new ScoreQueryCriteria(courseId,projectGrupId,userSessionData.getAccount(),userSessionData.getUserType(),pageIndex,number));
        return new QueryReturnDate(new PaginationData(number,page,((List<Integer>) scores.get(1)).get(0)),(List<GroupScore>)scores.get(0));
    }
}
